package Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import junit.framework.Assert;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import ControlLayer.PersonCtr;
import ControlLayer.RBLineCtr;
import ControlLayer.RoomBookingCtr;
import ControlLayer.RoomTypeCtr;
import ControlLayer.SaleCtr;
import DBLayer.DBRBLine;
import DBLayer.DBRoomBooking;
import DBLayer.DBSale;

import ModelLayer.RoomBookingLine;

public class RBLineCtrTest {
	private RBLineCtr rblineCtr;
	private RoomBookingCtr rbCtr;
	private SaleCtr saleCtr;
	private PersonCtr personCtr;
	private Date thedate1;
	private Date thedate2;
	private RoomTypeCtr rtCtr;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		rtCtr = new RoomTypeCtr();
		rblineCtr = new RBLineCtr();
		rbCtr = new RoomBookingCtr();
		saleCtr = new SaleCtr();
		personCtr = new PersonCtr();
		thedate1 = new SimpleDateFormat("dd/MM/yyyy").parse("01/07/2013");
		thedate2 = new SimpleDateFormat("dd/MM/yyyy").parse("02/07/2013");
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testInsertAndGetLine() {
		int rtId = rtCtr.insertRoomType("DoubleRoom", 100, 2);
		int guestId = personCtr.createGuest("Sam", "Smith", "Oktobervej 1",
				"Denmark", "43567890", "asjd@hotmail.com", "Guest", "A12366",
				0, 0);
		int employeeId = personCtr.createEmployee("Fay", "Stay", "SunnyRoad 4",
				"Morroco", "4563456", "stjy@gmail.com", "Employee",
				"23565589535", "Receptionist");
		int rbId = 0;
		try {
			rbId = saleCtr.insertSale2(guestId, employeeId, 100.0,
					"SaleUnpaid", "31/01/2013", "01/02/2013", "CreditCard");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		rbCtr.insertRoomBooking(rbId, thedate1, thedate1, thedate2, 50.0,
				100.0, "DepositUnpaid");
		int lineId = rblineCtr.insertLine(rbId, rtId, 1, 100);
		RoomBookingLine rbl = rblineCtr.getLine(rbId, lineId);
		Assert.assertEquals(rbId, rbl.getRoomBooking().getId());
		Assert.assertEquals(lineId, rbl.getId());
		Assert.assertEquals(1, rbl.getQuantity());
		Assert.assertEquals(rtId, rbl.getRoomType().getId());
		Assert.assertEquals(100.0, rbl.getTotalPrice());
		DBRBLine dbrbl = new DBRBLine();
		dbrbl.deleteLinesForRB(rbId);
		DBRoomBooking dbRB = new DBRoomBooking();
		dbRB.deleteRoomBooking(rbId);
		DBSale dbsale = new DBSale();
		dbsale.deleteSale(rbId);
		personCtr.deleteGuest(guestId);
		personCtr.deleteEmployee(employeeId);
		rtCtr.deleteRoomType(rtId);
	}

	@Test
	public void testUpdateLine() {
		int rtId1 = rtCtr.insertRoomType("DoubleRoom", 100, 2);
		int rtId2 = rtCtr.insertRoomType("SingleRoom", 80, 1);
		int guestId = personCtr.createGuest("Sam", "Smith", "Oktobervej 1",
				"Denmark", "43567890", "asjd@hotmail.com", "Guest", "A12366",
				0, 0);
		int employeeId = personCtr.createEmployee("Fay", "Stay", "SunnyRoad 4",
				"Morroco", "4563456", "stjy@gmail.com", "Employee",
				"23565589535", "Receptionist");
		int rbId = 0;
		try {
			rbId = saleCtr.insertSale2(guestId, employeeId, 100.0,
					"SaleUnpaid", "31/01/2013", "01/02/2013", "CreditCard");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		rbCtr.insertRoomBooking(rbId, thedate1, thedate1, thedate2, 50.0,
				100.0, "DepositUnpaid");
		int lineId = rblineCtr.insertLine(rbId, rtId1, 1, 100);
		rblineCtr.updateLine(rbId, lineId, rtId2, 2, 160);
		RoomBookingLine rbl = rblineCtr.getLine(rbId, lineId);
		Assert.assertEquals(rbId, rbl.getRoomBooking().getId());
		Assert.assertEquals(lineId, rbl.getId());
		Assert.assertEquals(rtId2, rbl.getRoomType().getId());
		Assert.assertEquals(2, rbl.getQuantity());
		Assert.assertEquals(160.0, rbl.getTotalPrice());
		DBRBLine dbrbl = new DBRBLine();
		dbrbl.deleteLinesForRB(rbId);
		DBRoomBooking dbRB = new DBRoomBooking();
		dbRB.deleteRoomBooking(rbId);
		DBSale dbsale = new DBSale();
		dbsale.deleteSale(rbId);
		personCtr.deleteGuest(guestId);
		personCtr.deleteEmployee(employeeId);
		rtCtr.deleteRoomType(rtId1);
		rtCtr.deleteRoomType(rtId2);
	}

	@Test
	public void testDeleteAllLineForRB() {
		int rtId = rtCtr.insertRoomType("DoubleRoom", 100, 2);
		int guestId = personCtr.createGuest("Sam", "Smith", "Oktobervej 1",
				"Denmark", "43567890", "asjd@hotmail.com", "Guest", "A12366",
				0, 0);
		int employeeId = personCtr.createEmployee("Fay", "Stay", "SunnyRoad 4",
				"Morroco", "4563456", "stjy@gmail.com", "Employee",
				"23565589535", "Receptionist");
		int rbId = 0;
		try {
			rbId = saleCtr.insertSale2(guestId, employeeId, 100.0,
					"SaleUnpaid", "31/01/2013", "01/02/2013", "CreditCard");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		rbCtr.insertRoomBooking(rbId, thedate1, thedate1, thedate2, 50.0,
				100.0, "DepositUnpaid");
		rblineCtr.insertLine(rbId, rtId, 1, 100);
		rblineCtr.insertLine(rbId, rtId, 1, 100);
		Assert.assertEquals(2, rblineCtr.deleteAllLineForRB(rbId));

		DBRoomBooking dbRB = new DBRoomBooking();
		dbRB.deleteRoomBooking(rbId);
		DBSale dbsale = new DBSale();
		dbsale.deleteSale(rbId);
		personCtr.deleteGuest(guestId);
		personCtr.deleteEmployee(employeeId);
		rtCtr.deleteRoomType(rtId);

	}

}
